package com.haier.cloudremote;

import java.util.ArrayList;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.content.SharedPreferences.Editor;
import android.content.res.XmlResourceParser;
import android.graphics.Bitmap;
import android.graphics.Color;
import android.graphics.drawable.BitmapDrawable;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.util.DisplayMetrics;
import android.view.KeyEvent;
import android.view.View;
import android.view.Window;
import android.view.View.OnClickListener;
import android.view.View.OnFocusChangeListener;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.AdapterView.OnItemSelectedListener;
import android.widget.Button;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.TextView;

import com.haier.cloudremote.adapter.CityAdapter;
import com.haier.cloudremote.adapter.ProvinceAdapter;
import com.haier.cloudremote.adapter.TownAdapter;
import com.haier.cloudremote.common.CityHelp;
import com.haier.cloudremote.common.SLog;
import com.haier.cloudremote.common.StaticData;
import com.haier.cloudremote.common.TroubleShooting;
import com.haier.cloudremote.common.XmlParser;
import com.haier.cloudremote.model.City;
import com.haier.cloudremote.model.Province;
import com.haier.cloudremote.model.StbList;
import com.haier.cloudremote.model.Town;
import com.haier.cloudremote.protocal.CAEProtocal;
import com.haier.cloudremote.util.FileUtil;

public class TestListViewActivity extends Activity implements OnFocusChangeListener,
	OnItemSelectedListener,OnItemClickListener,OnClickListener {
	
	private StringBuffer debugBuffer = null;// 记录debug按键
	
	//  以下为线程通信用的常量
	private static final int GET_TV_LOC_OK = 10001;
	private static final int GET_CAE_LOC_OK = 10002;
	private static final int GET_LOC_FAIL = 10003;
	
	// 以下为控件
	private ListView lvCommonListView, lvCommonListView2, lvCommonListView3;
	private Button imageButton, imageButton2, imageButton3;
	private TextView state_TextView,textView,textView1;
	private LinearLayout layout;
	
	private ArrayList<View> itemViews = new ArrayList<View>();
	private ArrayList<View> itemViews2 = new ArrayList<View>();
	private ArrayList<View> itemViews3 = new ArrayList<View>();
	
	public static ArrayList<Province> provinces;
	public static ArrayList<City> cities;
	public static ArrayList<Town> towns;
	
	public static Province checkedProvince;
	public static City checkedCity;
	public static Town checkedTown;
	public static String checkedText, checkedText2, checkedText3;
	
	private Handler viewHandler;
	
	private int gettedPId,gettedCityId,gettedTownId;
	private Boolean haveGetLoc = false;
	
	private int check_margintop, item_height, checkitem_height, item_textsize, checkitem_textsize;
	
	@Override
	protected void onCreate(Bundle savedInstanceState){	
		super.onCreate(savedInstanceState);
        this.requestWindowFeature(Window.FEATURE_NO_TITLE);
		setContentView(R.layout.main);
		
		ActivityStackControlUtil.add(this); 
		
		viewInit();
		
		viewHandler = new Handler(){

			@Override
			public void handleMessage(Message msg) {
				// TODO Auto-generated method stub
				super.handleMessage(msg);
				switch (msg.what) {
				case GET_TV_LOC_OK:// 获取到gettedCityId
					haveGetLoc = true;
					// 根据城市获得省
					int province_size = provinces.size();
					boolean havefindcity = false;
		    		for(int i=0;i<province_size;i++){
		    			Province province = provinces.get(i);
		    			if(province.getPid()!=0){
		    				cities = province.getCities(); 
			    			SLog.d("ssc", "TestListViewActivity----GET_TV_LOC_OK----province,cities=="+province.getPname()+","+cities);
			    			if(cities!=null){
			    				int cities_size = cities.size();
			            		
				            	for(int j=0;j<cities_size;j++){
				            		
				            		City city = cities.get(j);  
				            		SLog.d("ssc", "city=="+city.getCityId()+","+city.getCityName());
				            		
				            		if(city.getCityId()==gettedCityId){ 
				            			havefindcity = true;
				            			adaptiveOfCity(cities);
				            			adaptiveOfTown(city.getTowns());
				            			checkedProvince = province;
				            			checkedText = checkedProvince.getPname();				    
				            			checkedCity = city;
				            			checkedText2 = checkedCity.getCityName();
				            			imageButton.setText(checkedText);
				            			imageButton2.setText(checkedText2);
				            			break;
				            		}
				            	}
				            	if(havefindcity){
				            		break;
				            	}
			    			}else{
			    				continue;
			    			}
			    			
		    			}else{
		    				continue;
		    			}	    			
		    			
		    			
		    		}
		    		state_TextView.setText(R.string.getcityok);
		    		
				case GET_CAE_LOC_OK:// 获取到gettedPId,gettedCityId
					haveGetLoc = true;
					province_size = provinces.size();
					SLog.d("ssc", "province_size==="+province_size);
		    		for(int i=0;i<province_size;i++){
		    			Province province = provinces.get(i);
		    			if(province.getPid()==gettedPId){
		    				lvCommonListView.setSelection(i);
		    				SLog.d("ssc", "province==="+province.getPname());
		    				cities = province.getCities();
		    				SLog.d("ssc", "cities==="+cities);
		    				if(cities!=null){
		    					int cities_size = cities.size();
			    				for(int j=0;j<cities_size;j++){
			    					City city = cities.get(j);
			    					if(city.getCityId()==gettedCityId){
			    						lvCommonListView2.setSelection(j);
			    						adaptiveOfTown(city.getTowns());
			    					}	
			    					break;
			    				}	   
		    				}
		    				 						    				
		    				break;
		    			}
		    		}				
					state_TextView.setText(R.string.getcityok);
					break;
				case GET_LOC_FAIL:// 获取区域失败，提示用户手动选择
					haveGetLoc = true;
					state_TextView.setText(R.string.getcityfail);
					break;

				default:
					break;
				}
			}
			
		};
        
		// 获取上次选择的省市区
		SLog.d("ssc","获取上次选择的省市区");
		SharedPreferences sharedata = getSharedPreferences("ssc", Context.MODE_PRIVATE);  
	    gettedPId = sharedata.getInt("checkedPId", 0); 
	    gettedCityId = sharedata.getInt("checkedCityId", 0);
	    gettedTownId = sharedata.getInt("checkedTownId", 0);
	    SLog.d("ssc","gettedPId=="+gettedPId+" gettedCityId=="+gettedCityId+" gettedTownId=="+gettedTownId);
	    if(gettedPId==0 || gettedCityId==0 || gettedTownId==0){
	    	getLocFromCAE();
	    }else{
	        haveGetLoc = true;
	        state_TextView.setVisibility(View.GONE);
	    	// 将用户选择的省市区置为选中
		    int province_size = provinces.size();
		    Province province;
		    City city;
		    Town town;
		    for(int i=0;i<province_size;i++){
		    	province = provinces.get(i);
		    	
		    	if(province.getPid()==gettedPId){// 在省列表中匹配获得的省
		    		checkedProvince = province;
		    		checkedText = checkedProvince.getPname();
		    		imageButton.setText(checkedText);
		    		lvCommonListView.setSelection(i);// 选中列表中该省
		    		SLog.d("ssc", "province==="+province.getPname());
		    		cities = province.getCities();
		    		SLog.d("ssc", "cities==="+cities);
		    		int cities_size = cities.size();
		    		
	    			for(int j=0;j<cities_size;j++){// 在市列表中匹配获得的市
	    				city = cities.get(j);	    				
	    				if(city.getCityId()==gettedCityId){
	    					checkedCity = city;
	    					checkedText2 = checkedCity.getCityName();
	    					SLog.d("ssc", "city==="+checkedText2);
	    					imageButton2.setText(checkedText2);
	    					lvCommonListView2.setSelection(j);// 选中列表中该市
	    					towns = city.getTowns();
	    					int town_size = towns.size();
	    					for(int k=0;k<town_size;k++){// 在区列表中匹配获得的区
	    						town = towns.get(k);
	    						if(town.getTownId()==gettedTownId){
	    							checkedTown = town;
	    							town = null;
	    							province = null;
	    							city = null;
	    							checkedText3 = checkedTown.getTownName();
	    							SLog.d("ssc", "town==="+checkedText3);
	    							imageButton3.setText(checkedText3);
	    							lvCommonListView3.setSelection(k);// 选中列表中该区
	    								
//	    							nextButton.requestFocus();
	    							break;
	    						}else{
	    							if(k==town_size-1){
	    								checkedTown = towns.get(0);
		    							checkedText3 = checkedTown.getTownName();
		    							SLog.d("ssc", "town==="+checkedText3);
		    							imageButton3.setText(checkedText3);
		    								
//		    							nextButton.requestFocus();
	    							}
	    						}
	    					}
	    					break;
	    				}else{
	    					if(j==cities_size-1){
	    						checkedCity = province.getCities().get(0);
		    					checkedText2 = checkedCity.getCityName();
		    					SLog.d("ssc", "city==="+checkedText2);
		    					imageButton2.setText(checkedText2);
		    					lvCommonListView2.setSelection(j);// 选中列表中该市
		    					towns = checkedCity.getTowns();
		    					checkedTown = towns.get(0);
		    					checkedText3 = checkedTown.getTownName();
    							SLog.d("ssc", "town==="+checkedText3);
    							imageButton3.setText(checkedText3);
	    					}
	    				}	    					
	    			}
	    			break;
	    		}
	    	}		
		    adaptiveOfCity(cities);
		    adaptiveOfTown(towns);
	    }

//		getLocation();
	}

	/**
	 * 获取区域信息
	 */
	private void getLocFromCAE() {
		// TODO Auto-generated method stub
    	state_TextView.setText(R.string.getting_city);
//    	nextButton.setEnabled(false);
    	new Thread(){

			@Override
			public void run() {
				// TODO Auto-generated method stub
				super.run();
//				gettedCityId = Integer.parseInt(CityHelp.getCityId(TestListViewActivity.this));// 获取城市设置
				SLog.d("ssc", "获取城市设置");
		        if(gettedCityId!=0){
		        	viewHandler.sendEmptyMessage(GET_TV_LOC_OK);
		        	SLog.d("ssc", "获取到TV设置的城市为gettedCityId："+gettedCityId);
		    	}else{
		    		SLog.d("ssc", "向CAE获取城市");
		    		// 向CAE获取城市
			        StbList stbList = CAEProtocal.getStbListFromCAE(0, 0, 0, "");
			        if(stbList!=null){
			        	SLog.d("ssc", "已获取到StbList");
			        	gettedPId = stbList.getpId();
			            gettedCityId = stbList.getCityId();
			            SLog.d("ssc", "----------------gettedPId=="+gettedPId+" gettedCityId=="+gettedCityId);
			            if(gettedPId!=0 && gettedCityId!=0){
			            	viewHandler.sendEmptyMessage(GET_CAE_LOC_OK);
			            }else{
			            	viewHandler.sendEmptyMessage(GET_LOC_FAIL);
			            }
			            
			        }else{
			        	viewHandler.sendEmptyMessage(GET_LOC_FAIL);
			        }			        
		    	}		        		        
			}    		
    	}.start();
    	
	}

	/**
	 * 初始化界面控件
	 */
	private void viewInit() {
		check_margintop = (int) getResources().getDimension(R.dimen.list_check_margintop);
		item_height = (int)getResources().getDimension(R.dimen.list_item_height);
		checkitem_height = (int) getResources().getDimension(R.dimen.list_checkitem_height);
		item_textsize = (int) getResources().getDimension(R.dimen.list_item_textsize);
		checkitem_textsize = (int) getResources().getDimension(R.dimen.list_checkitem_textsize);
		
		layout = (LinearLayout) findViewById(R.id.loc_layout);
		lvCommonListView = (ListView) findViewById(R.id.lvCommonListView);
	
		imageButton = (Button) findViewById(R.id.imagebutton);
		imageButton.setOnFocusChangeListener(this);
		
		lvCommonListView2 = (ListView) findViewById(R.id.lvCommonListView2);
		imageButton2 = (Button) findViewById(R.id.imagebutton2);
		imageButton2.setOnFocusChangeListener(this);
		
		lvCommonListView3 = (ListView) findViewById(R.id.lvCommonListView3);
		imageButton3 = (Button) findViewById(R.id.imagebutton3);
		imageButton3.setOnFocusChangeListener(this);
		
		state_TextView = (TextView) findViewById(R.id.state_textView);
		
		lvCommonListView.setOnFocusChangeListener(this);
		lvCommonListView2.setOnFocusChangeListener(this);
		lvCommonListView3.setOnFocusChangeListener(this);
		
	    // 获取资源中的城市编码XML文件并对文件进行解析，获取到所有省市的列表
	    XmlResourceParser xrp = this.getResources().getXml(R.xml.localencode);
	    StaticData.provinces = XmlParser.getProvinceList(xrp);
	    adaptiveOfProvince(StaticData.provinces,gettedPId,gettedCityId);
	    xrp = null;
		  
	    lvCommonListView.setOnItemSelectedListener(this);		
		lvCommonListView.setOnItemClickListener(this);
		imageButton3.requestFocus();
	}
	
	@Override
	protected void onStart() {
		// TODO Auto-generated method stub
		super.onStart();
		Bitmap bm = FileUtil.readBitMap(this, R.drawable.localbg);
		layout.setBackgroundDrawable(new BitmapDrawable(bm));
	}

	/**
	 * 适配省列表
	 * @param pros 所有省的列表
	 * @param currProId 当前省ID
	 * @param currCitId 当前市ID
	 */
	private void adaptiveOfProvince(ArrayList<Province> pros, int currProId, int currCitId) {
		// TODO Auto-generated method stub	
		SLog.d("test", "adaptiveOfProvince===");
		provinces = new ArrayList<Province>();
		Province pro;
		for(int i=0;i<10;i++){// 先增加10个空的省
			pro = new Province();
			pro.setPid(0);
			pro.setPname("");
			provinces.add(pro);
		}
		int prosSize = pros.size();
		for(int i=0;i<prosSize;i++){// 添加所有省
			if(i==0){
				
				checkedProvince = pros.get(i);
				SLog.d("addfs", "adaptiveOfProvince===checkedProvince==="+checkedProvince.getPname());
				checkedText = checkedProvince.getPname();
				imageButton.setText(checkedText);
				cities = checkedProvince.getCities();
				
				checkedCity = cities.get(0);
				checkedText2 = checkedCity.getCityName();
				imageButton2.setText(checkedText2);
				
				towns = checkedCity.getTowns();
				checkedTown = towns.get(0);
				checkedText3 = checkedTown.getTownName();
				imageButton3.setText(checkedText3);
			} 
			else {
				if(pros.get(i).getPid()==currProId){
					checkedProvince = pros.get(i);
					SLog.d("addfs", "adaptiveOfProvince===checkedProvince==="+checkedProvince.getPname());
					checkedText = checkedProvince.getPname();
					imageButton.setText(checkedText);
					
					ArrayList<City> cits = checkedProvince.getCities();
					int cityPos = 0;
					for(int j=0;j<cits.size();j++){
						if(cits.get(j).getCityId()==currCitId){
							cityPos = j;						
							break;
						}					
					}
					checkedCity = cities.get(cityPos);
					checkedText2 = checkedCity.getCityName();
					imageButton2.setText(checkedText2);
					
					towns = checkedCity.getTowns();
					checkedTown = towns.get(0);
					checkedText3 = checkedTown.getTownName();
					imageButton3.setText(checkedText3);
					
				}
			}

			provinces.add(pros.get(i));
		}
		for(int i=0;i<10;i++){// 在后面增加10个空的省
			pro = new Province();
			pro.setPid(0);
			pro.setPname("");
			provinces.add(pro);
		}
		
		// 适配省的下拉列表
		ProvinceAdapter provinceAdapter = new ProvinceAdapter(this, provinces);
		lvCommonListView.setAdapter(provinceAdapter);
		lvCommonListView.setSelection(0);		
		lvCommonListView3.setOnItemSelectedListener(this);
		lvCommonListView3.setOnItemClickListener(this);
		lvCommonListView3.setOnFocusChangeListener(this);
	}
	
	/**
	 * 适配市列表
	 * @param cities
	 */
	private void adaptiveOfCity(ArrayList<City> cis) {
		// TODO Auto-generated method stub	
		SLog.d("test", "adaptiveOfCity");
		cities = new ArrayList<City>();
		City city;
		for(int i=0;i<10;i++){// 先增加10个空的市
			city = new City();
			city.setCityId(0);
			city.setCityName("");
			cities.add(city);
		}
		int citiesSize = cis.size();
		SLog.d("test", "cis.size=="+citiesSize);
		for(int i=0;i<citiesSize;i++){// 添加所有市
			cities.add(cis.get(i));
		}
		for(int i=0;i<10;i++){// 在后面增加10个空的市
			city = new City();
			city.setCityId(0);
			city.setCityName("");
			cities.add(city);
		}
		
		// 适配省的下拉列表
		CityAdapter cityAdapter = new CityAdapter(this, cities);
//		SLog.d("test", "cities.size=="+cities.size());
		lvCommonListView2.setAdapter(cityAdapter);	

	}
	
	/**
	 * 适配区列表
	 * @param towns
	 */
	private void adaptiveOfTown(ArrayList<Town> tos) {
		towns = new ArrayList<Town>();
		if(tos!=null && tos.size()>0){
			checkedTown = tos.get(0);
			checkedText3 = checkedTown.getTownName();
			imageButton3.setText(checkedText3);
		}
		
		Town town;
		for(int i=0;i<10;i++){// 先增加10个空的市
			town = new Town();
			town.setTownId(0);
			town.setTownName("");
			towns.add(town);
		}
		int townsSize = tos.size();
		for(int i=0;i<townsSize;i++){// 添加所有市
			towns.add(tos.get(i));
		}
		for(int i=0;i<10;i++){// 在后面增加10个空的市
			town = new Town();
			town.setTownId(0);
			town.setTownName("");
			towns.add(town);
		}
		
		// 适配省的下拉列表
		TownAdapter townAdapter = new TownAdapter(this, towns);
		lvCommonListView3.setAdapter(townAdapter);

	}

	@Override
	public void onFocusChange(View v, boolean hasFocus) {
		// TODO Auto-generated method stub
		SLog.d("event", "onFocusChange------v=="+v.getId()+" "+v.getClass().getName()+" hasFocus="+hasFocus);
		
		switch (v.getId()) {
		case R.id.imagebutton:
			if(hasFocus){				
				SLog.d("event", "onFocusChange---------imagebutton.hasFocus");

				lvCommonListView.setVisibility(View.VISIBLE);
				lvCommonListView.requestFocus();
				imageButton.setVisibility(View.GONE);				
				
				for(int i=0;i<provinces.size();i++){
					if(provinces.get(i).getPid()==checkedProvince.getPid()){
						lvCommonListView.setSelectionFromTop(i, check_margintop);
						break;
					}
				}
			}
			break;
		case R.id.lvCommonListView:
			if(!hasFocus){
				SLog.d("event", "onFocusChange---------lvCommonListView.!hasFocus");
				lvCommonListView.setVisibility(View.GONE);
				imageButton.setVisibility(View.VISIBLE);
				SLog.d("test", "onFocusChange---lvCommonListView.noFocus");
				SLog.d("test", "onFocusChange---checkedProvince=="+checkedProvince.getPname());
				SLog.d("test", "onFocusChange---checkedText=="+checkedText);
				imageButton.setText(checkedText);
			}else{
				state_TextView.setVisibility(View.INVISIBLE);
				imageButton2.setFocusable(true);
				imageButton3.setFocusable(true);
			}
			break;
		case R.id.imagebutton2:
			if(hasFocus){
				SLog.d("event", "onFocusChange---------imagebutton2.hasFocus");
				
				lvCommonListView2.setVisibility(View.VISIBLE);
				adaptiveOfCity(checkedProvince.getCities());
				lvCommonListView2.setOnItemSelectedListener(this);
				lvCommonListView2.setOnItemClickListener(this);
				
				for(int i=0;i<cities.size();i++){
					if(cities.get(i).getCityId()==checkedCity.getCityId()){
						lvCommonListView2.setSelection(i);
						break;
					}
				}
				
				lvCommonListView2.requestFocus();
				imageButton2.setVisibility(View.GONE);
			}
			break;
		case R.id.lvCommonListView2:
			if(!hasFocus){
				SLog.d("event", "onFocusChange---------lvCommonListView2.!hasFocus");
				lvCommonListView2.setVisibility(View.GONE);
				imageButton2.setVisibility(View.VISIBLE);
				imageButton2.setText(checkedText2);
			}else{
				state_TextView.setVisibility(View.INVISIBLE);
			}
			break;
		case R.id.imagebutton3:
			if(hasFocus){
				SLog.d("event", "onFocusChange---------imagebutton3.hasFocus");
				
				lvCommonListView3.setVisibility(View.VISIBLE);
				
				adaptiveOfTown(checkedCity.getTowns());
				lvCommonListView3.setOnItemSelectedListener(this);

				lvCommonListView3.requestFocus();
				imageButton3.setVisibility(View.GONE);
				
				for(int i=0;i<towns.size();i++){
					if(towns.get(i).getTownId()==checkedTown.getTownId()){
						SLog.d("event", "onKeyDown-------towns.get(i).getTownId()==checkedTown.getTownId()=="+checkedTown.getTownId());
						lvCommonListView3.setSelection(i);
						SLog.d("event", "lvCommonListView3.setSelection(i);");
						break;
					}
				}
			
			}
			break;
		case R.id.lvCommonListView3:
			if(!hasFocus){
				SLog.d("event", "onFocusChange---------lvCommonListView3.!hasFocus");
				lvCommonListView3.setVisibility(View.GONE);
				imageButton3.setVisibility(View.VISIBLE);
				imageButton3.setText(checkedText3);
			}
			break;
		default:
			break;
		}
	}

	@Override
	public void onItemSelected(AdapterView<?> arg0, View v, int arg2,
			long arg3) {
		// TODO Auto-generated method stub
		SLog.d("event", "onItemSelected");
		if(v!=null){
			switch (arg0.getId()) {
			case R.id.lvCommonListView:
				SLog.d("event", "onItemSelected---------lvCommonListView----arg2=="+arg2);
				itemViews.add(v);
				textView1 = (TextView) v.getTag();
				textView1.setTextColor(Color.WHITE);
				textView1.setTextSize(checkitem_textsize);
				textView1.getLayoutParams().height = checkitem_height;
				
				if(arg2<=10){
					lvCommonListView.setSelectionFromTop(10, check_margintop);					
					checkedProvince = provinces.get(10);
					SLog.d("addfs", "onItemSelected----checkedProvince=="+checkedProvince.getPname());
				}else if(arg2>provinces.size()-1-10){
					lvCommonListView.setSelectionFromTop(provinces.size()-1-10, check_margintop);
					checkedProvince = provinces.get(provinces.size()-1-10);
					SLog.d("addfs", "onItemSelected----checkedProvince=="+checkedProvince.getPname());
				}else{
					lvCommonListView.setSelectionFromTop(arg2, check_margintop);
					checkedProvince = provinces.get(arg2);
					SLog.d("addfs", "onItemSelected----checkedProvince=="+checkedProvince.getPname());
				}
				checkedText = checkedProvince.getPname();
				
				for(int i=0;i<itemViews.size();i++){
					if(itemViews.get(i)==v){
						SLog.d("test", "itemViews.get("+i+")==v");
						TextView textView = (TextView) v.getTag();
						textView.setTextColor(Color.WHITE);
						textView.setTextSize(checkitem_textsize);
//						textView.setHeight(200);
						textView.getLayoutParams().height = checkitem_height;
					}else{
						TextView textView = (TextView) itemViews.get(i).getTag();
						textView.setTextColor(Color.BLACK);
						textView.setTextSize(item_textsize);
//						textView.setHeight(60);
						textView.getLayoutParams().height = item_height;
					}
				}
				break;
			case R.id.lvCommonListView2:
				SLog.d("event", "onItemSelected---------lvCommonListView2----arg2=="+arg2);
				itemViews2.add(v);
				if(arg2<=10){
					lvCommonListView2.setSelectionFromTop(10, check_margintop);
					checkedCity = cities.get(10);
					checkedText2 = checkedCity.getCityName();
				}else if(arg2>cities.size()-1-10){
					lvCommonListView2.setSelectionFromTop(cities.size()-1-10, check_margintop);
					checkedCity = cities.get(cities.size()-1);
					checkedText2 = checkedCity.getCityName();
				}else{
					lvCommonListView2.setSelectionFromTop(arg2, check_margintop);
					checkedCity = cities.get(arg2);
					checkedText2 = checkedCity.getCityName();
				}
				
				for(int i=0;i<itemViews2.size();i++){
					if(itemViews2.get(i)==v){
						SLog.d("test", "itemViews2.get("+i+")==v");
						textView = (TextView) v.getTag();
						textView.setTextColor(Color.WHITE);
						textView.setTextSize(checkitem_textsize);
						textView.getLayoutParams().height = checkitem_height;
					}else{
						textView = (TextView) itemViews2.get(i).getTag();
						textView.setTextColor(Color.BLACK);
						textView.setTextSize(item_textsize);
						textView.getLayoutParams().height = item_height;
					}
				}
				break;

			case R.id.lvCommonListView3:
				SLog.d("event", "onItemSelected---------lvCommonListView3----arg2=="+arg2);
				itemViews3.add(v);
				if(arg2<=10){
					SLog.d("event", "onItemSelected---------lvCommonListView3----arg2<=10");
					lvCommonListView3.setSelectionFromTop(10, check_margintop);
					checkedTown = towns.get(10);
					checkedText3 = checkedTown.getTownName();
				}else if(arg2>towns.size()-1-10){
					SLog.d("event", "onItemSelected---------lvCommonListView3----towns.size()-1-10---"+(towns.size()-1-10));
					lvCommonListView3.setSelectionFromTop(towns.size()-1-10, check_margintop);
					checkedTown = towns.get(towns.size()-1);
					checkedText3 = checkedTown.getTownName();
				}else{
					SLog.d("event", "onItemSelected---------lvCommonListView3----else---");
					lvCommonListView3.setSelectionFromTop(arg2, check_margintop);
					checkedTown = towns.get(arg2);
					checkedText3 = checkedTown.getTownName();
				}
				
				for(int i=0;i<itemViews3.size();i++){
					if(itemViews3.get(i)==v){
						SLog.d("test", "itemViews3.get("+i+")==v");
						textView = (TextView) v.getTag();
						textView.setTextColor(Color.WHITE);
						textView.setTextSize(checkitem_textsize);
						textView.getLayoutParams().height = checkitem_height;
					}else{
						textView = (TextView) itemViews3.get(i).getTag();
						textView.setTextColor(Color.BLACK);
						textView.setTextSize(item_textsize);
						textView.getLayoutParams().height = item_height;
					}
				}
				break;
			default:
				break;
			}
		}
		
	}

	@Override
	public void onNothingSelected(AdapterView<?> arg0) {
		// TODO Auto-generated method stub
		
	}

	@Override
	public void onItemClick(AdapterView<?> arg0, View v, int arg2, long arg3) {
		// TODO Auto-generated method stub
		SLog.d("test", "onItemClick");
		if(v!=null){
			switch (arg0.getId()) {
			case R.id.lvCommonListView:
				SLog.d("event", "onItemClick-----------lvCommonListView---arg2=="+arg2);
				if(arg2<=10){
					checkedProvince = provinces.get(10);	
					SLog.d("addfs", "onItemClick----checkedProvince=="+checkedProvince.getPname());
				}else if(arg2>provinces.size()-1-10){
					checkedProvince = provinces.get(provinces.size()-1-10);
					SLog.d("addfs", "onItemClick----checkedProvince=="+checkedProvince.getPname());
				}else{
					checkedProvince = provinces.get(arg2);		
					SLog.d("addfs", "onItemClick----checkedProvince=="+checkedProvince.getPname());
				}
				checkedText = checkedProvince.getPname();
				imageButton2.requestFocus();
				
				break;
			case R.id.lvCommonListView2:
				SLog.d("event", "onItemClick-----------lvCommonListView2---arg2=="+arg2);
				if(arg2<=10){
					checkedCity = cities.get(10);
					checkedText2 = checkedCity.getCityName();
				}else if(arg2>cities.size()-1-10){
					checkedCity = cities.get(cities.size()-1);
					checkedText2 = checkedCity.getCityName();
				}else{
					checkedCity = cities.get(arg2);
					checkedText2 = checkedCity.getCityName();
				}
				imageButton3.requestFocus();

				break;

			case R.id.lvCommonListView3:
				SLog.d("event", "onItemClick-----------lvCommonListView3---arg2=="+arg2);
				if(arg2<=10){
					checkedTown = towns.get(10);
					checkedText3 = checkedTown.getTownName();
				}else if(arg2>towns.size()-1-10){
					checkedTown = towns.get(towns.size()-1);
					checkedText3 = checkedTown.getTownName();
				}else{
					checkedTown = towns.get(arg2);
					checkedText3 = checkedTown.getTownName();
				}
				if(haveGetLoc){
					Intent intent = new Intent();
					intent.putExtra("loctext", checkedText+checkedText2+checkedText3);
					intent.setClass(TestListViewActivity.this, GetStbLoadActivity.class);
					startActivity(intent);
					finish();
				}

				break;
			default:
				break;
			}
		}
		
		
	}

	@Override
	public void onClick(View v) {
		// TODO Auto-generated method stub
		SLog.d("test", "onClick------------v=="+v);
		switch (v.getId()) {
		case R.id.imagebutton:
			imageButton.setVisibility(View.INVISIBLE);
			lvCommonListView.setVisibility(View.VISIBLE);
			lvCommonListView.requestFocus();			
			break;

		case R.id.imagebutton2:
			imageButton2.setVisibility(View.INVISIBLE);
			lvCommonListView2.setVisibility(View.VISIBLE);
			cities = checkedProvince.getCities();
			SLog.d("test", "onClick***cities.size=="+cities.size());
			adaptiveOfCity(cities);
			lvCommonListView2.requestFocus();
			
			break;

		case R.id.imagebutton3:
			imageButton3.setVisibility(View.INVISIBLE);
			lvCommonListView3.setVisibility(View.VISIBLE);
			towns = checkedCity.getTowns();
			SLog.d("test", "towns.size=="+towns.size());
			adaptiveOfTown(towns);
			lvCommonListView3.requestFocus();
			
			break;
		default:
			break;
		}
	}
	
	@Override
	protected void onPause() {
		// TODO Auto-generated method stub
		super.onPause();
		
		StaticData.checkedProvince = checkedProvince;
        StaticData.checkedCity = checkedCity;
        StaticData.checkedTown = checkedTown;
        
	}

	@Override
	protected void onStop() {
		// TODO Auto-generated method stub
		super.onStop();
		SLog.d("ssc", "写配置文件 "+checkedProvince.getPid()+" "+checkedCity.getCityId()+" "+checkedTown.getTownId());
		 // 写配置文件
        Editor sharedata = getSharedPreferences("ssc", Context.MODE_PRIVATE).edit(); 

        // 本次选择的省市区
        sharedata.putInt("checkedPId", checkedProvince.getPid());
        sharedata.putInt("checkedCityId", checkedCity.getCityId());
        sharedata.putInt("checkedTownId", checkedTown.getTownId());
        sharedata.commit(); 
        sharedata = null;
        
        if(StaticData.checkedProvince==null){
        	StaticData.checkedProvince = checkedProvince;
            StaticData.checkedCity = checkedCity;
            StaticData.checkedTown = checkedTown;
        }
        layout.setBackgroundDrawable(null);
	}

	@Override
	protected void onDestroy() {
		// TODO Auto-generated method stub
		super.onDestroy();         
       
		 //移出管理栈
        ActivityStackControlUtil.remove(this);
	}

	@Override
	public boolean onKeyDown(int keyCode, KeyEvent event) {
		// TODO Auto-generated method stub
		switch (event.getKeyCode()) {
		case KeyEvent.KEYCODE_DPAD_RIGHT:
		case KeyEvent.KEYCODE_DPAD_CENTER:
		    SLog.d("ssc", "haveGetLoc=="+haveGetLoc);
		    SLog.d("ssc", "getCurrentFocus()=="+getCurrentFocus());
		    SLog.d("ssc", "lvCommonListView3=="+lvCommonListView3);
			if(getCurrentFocus()==lvCommonListView3 && haveGetLoc){
				Intent intent = new Intent();
				intent.putExtra("loctext", checkedText+checkedText2+checkedText3);
				intent.setClass(TestListViewActivity.this, GetStbLoadActivity.class);
				startActivity(intent);
				finish();
				return false;
			}
			break;
		case KeyEvent.KEYCODE_DPAD_LEFT:
			if(getCurrentFocus()==lvCommonListView){
				finish();
				return false;
			}
			break;
		case 12:// 5
			debugBuffer = new StringBuffer();
			debugBuffer.append("l");
			SLog.d("ssc", "11-----debugBuffer=="+debugBuffer);
			return false;

		case 13:// 6
			if(debugBuffer!=null){
				debugBuffer.append("o");
				SLog.d("ssc", "13-----debugBuffer=="+debugBuffer);
				return false;
			}
			break;

		case 11:// 4
			if(debugBuffer!=null){
				debugBuffer.append("g");
				TroubleShooting troubleShooting = new TroubleShooting(TestListViewActivity.this, debugBuffer);
				debugBuffer = new StringBuffer();

				return false;
			}
			
			break;
		default:
			break;
		}
		return super.onKeyDown(keyCode, event);
	}

}